//package com.example.apiversioncontroldemo.config;
//
//import com.example.apiversioncontroldemo.util.vcon.ApiVersion;
//import com.example.apiversioncontroldemo.util.vcon.ApiVersionCondition;
//import com.example.apiversioncontroldemo.util.vcon.RequestUtils;
//import lombok.AllArgsConstructor;
//import lombok.Getter;
//import lombok.Setter;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
//import org.springframework.web.servlet.mvc.condition.RequestCondition;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//
///**
// * @author chen.cr
// * @since 2023/10/19
// */
////@Configuration
////@ConfigurationProperties(prefix = "web-config")
//public class WebConfig extends WebMvcConfigurationSupport {
//
//    @Setter
//    @Getter
//    private int allowedMinVersion;
//
//    /**
//     * 注册 RequestMapping 路由处理器
//     *
//     * @return
//     */
//    @Override
//    protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
//        RequestMappingHandlerMapping requestMappingHandlerMapping = new CustomRequestMappingHandlerMapping();
//        requestMappingHandlerMapping.setOrder(0);
//        return requestMappingHandlerMapping;
//    }
//
//    /**
//     * 注册请求拦截器
//     *
//     * @param registry
//     */
//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new CustomWebRequestVersionCheckInterceptor(allowedMinVersion));
//    }
//
//
//    public static class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
//        /**
//         * @param handlerType 创建 RequestCondition 的处理类
//         * @return
//         */
//        @Override
//        protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
//            ApiVersion annotation = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
//            return annotation == null ? null : new ApiVersionCondition(annotation.version());
//        }
//
//        /**
//         * @param method 创建 RequestCondition 的处理方法
//         * @return
//         */
//        @Override
//        protected RequestCondition<?> getCustomMethodCondition(Method method) {
//            ApiVersion annotation = AnnotationUtils.findAnnotation(method, ApiVersion.class);
//            return annotation == null ? null : new ApiVersionCondition(annotation.version());
//        }
//
//    }
//
//
//    @AllArgsConstructor
//    public static class CustomWebRequestVersionCheckInterceptor implements HandlerInterceptor {
//
//        private int minVersion;
//
//        @Override
//        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//            Integer version = RequestUtils.findApiVersion(request);
//            if (version != null) {
//                if (version < minVersion) {
//                    // 直接返回error
//                    response.sendError(499, String.format("当前API版本[v%d]不兼容！最小可兼容版本为：v%d", version, minVersion));
//                }
//            }
//
//            return true;
//        }
//    }
//
//}
